#ifndef RTAPI_ATOMIC_H
#define RTAPI_ATOMIC_H

#if defined(__GNUC__) && ((__GNUC__ << 8) | __GNUC_MINOR__) >= 0x409
#define RTAPI_USE_STDATOMIC
#elif defined(__STDC_VERSION__) && __STDC_VERSION > 201112L
#define RTAPI_USE_STDATOMIC
#endif

#ifdef RTAPI_USE_STDATOMIC
#include <stdatomic.h>
#else

enum memory_order
{
    memory_order_relaxed,
    memory_order_consume,
    memory_order_acquire,
    memory_order_release,
    memory_order_acq_rel,
    memory_order_seq_cst
};

#define atomic_store(obj, desired) atomic_store_explicit((obj), (desired), memory_order_seq_cst)
#define atomic_load(obj) atomic_load_explicit((obj), memory_order_seq_cst)

// note that in this implementation, only one level of synchronization is supported, equivalent to memory_order_seq_cst
#define atomic_store_explicit(obj, desired, order) \
    ({                                             \
        (void)order;                               \
        __sync_synchronize();                      \
        *(obj) = (desired);                        \
        (void)0;                                   \
    })

#define atomic_load_explicit(obj, order) \
    ({                                   \
        (void)order;                     \
        __typeof__(*(obj)) v = *(obj);   \
        __sync_synchronize();            \
        v;                               \
    })

#endif

#endif
